﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Service.Valid.ExhibitionKpiValid;
using S4Store.Domain.ExhibitionKpi;
using S4Store.Domain.NetMarketingKpi;
using S4Store.Domain;
using S4Store.Service.Valid.NetMarketingKpiValid;

namespace S4Store.Service
{
    public class NetMarketingKpiService : BaseService
    {
        int comfirmstatus = DataConfirmStatus.Confirmed.ToInt();

        public S4Store.Service.Valid.NetMarketingMediaValid.Add ValidMediaAdd { get; set; }
        public S4Store.Service.Valid.NetMarketingMediaValid.Modify ValidMediaModify { get; set; }
        public S4Store.Service.Valid.NetMarketingMediaValid.Delete ValidMediaDelete { get; set; }

        public NetMarketingKpiMonthConfigSave NetMarketingKpiMonthConfigSave { get; set; }

        public IQueryable<NetMarketingMedia> GetMediaQuery()
        {
            return this.GetQuery<NetMarketingMedia>();
        }

        public IQueryable<NetMarketingMedia> GetMedias(int storeid)
        {
            return this.GetQuery<NetMarketingMedia>().Where(t=>t.StoreId==storeid);
        }

        public IQueryable<NetMarketingMedia> GetEnabledMedias(int storeid)
        {
            return this.GetQuery<NetMarketingMedia>().Where(t => t.StoreId == storeid && !t.IsClose);
        }

        public IQueryable<NetMarketingKpiMonthConfig> GetMonthConfigQuery()
        {
            return this.GetQuery<NetMarketingKpiMonthConfig>();
        }
        public IQueryable<NetMarketingKpiMediaMonthConfig> GetMediaMonthConfigQuery()
        {
            return this.GetQuery<NetMarketingKpiMediaMonthConfig>();
        }
        public IQueryable<NetMarketingKpiDayData> GetDataQuery()
        {
            return this.GetQuery<NetMarketingKpiDayData>();
        }

        public NetMarketingKpiMonthConfig GetMonthConfig(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                NetMarketingKpiMonthConfig config = this.GetMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
                if (config == null)
                {
                    config = new NetMarketingKpiMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
            else
            {
                NetMarketingKpiMonthConfig config = this.GetMonthConfigQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
                if (config == null)
                {
                    config = new NetMarketingKpiMonthConfig();
                    config.Month = new DateTime(year, month, 1);
                    config.StoreId = storeid;
                }
                return config;
            }
        }


        public void SubmitMonthConfig(NetMarketingKpiMonthConfig config)
        {
            this.Valid(config, NetMarketingKpiMonthConfigSave);
            if (config.Id == 0)
            {
                this.GetSet<NetMarketingKpiMonthConfig>().Add(config);
            }
        }


        public void SubmitMediaMonthConfig(int year, int month, int storeid, List<NetMarketingKpiMediaMonthConfig> configs)
        {
            var MediaMonthConfigs = GetMediaMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            foreach (var Carc in MediaMonthConfigs)
            {
                this.GetSet<NetMarketingKpiMediaMonthConfig>().Remove(Carc);
            }
            foreach (var config in configs)
            {
                this.GetSet<NetMarketingKpiMediaMonthConfig>().Add(config);
            }
        }


        public IQueryable<NetMarketingKpiMediaMonthConfig> GetMediaMonthConfigs(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetMediaMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }
            else
            {
                return GetMediaMonthConfigQuery().Where(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
            }
        }


        public NetMarketingKpiMediaMonthConfig GetMediaMonthConfig(int year, int month, int storeid, int mediaid, bool onlycomfirmd = false)
        {
            var config = GetMediaMonthConfigs(year, month, storeid, onlycomfirmd).FirstOrDefault(t => t.MediaId == mediaid);
            if (config == null)
            {
                config = new NetMarketingKpiMediaMonthConfig
                {
                     MediaId=mediaid, 
                    StoreId = storeid,
                    Month = new DateTime(year, month, 1),

                };
            }
            return config;
        }


        public IQueryable<NetMarketingKpiDayData> GetDayDatas(int year, int month, int storeid, bool onlycomfirmd = false)
        {
            if (onlycomfirmd)
            {
                return GetDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid && t.IsConfirmed);
            }

            else
            {
                return GetDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.StoreId == storeid);
            }
        }


        public IQueryable<NetMarketingKpiDayData> GetDayDatas(int year, int month, int day, int storeid)
        {
            return GetDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
        }

        public void SubmitDayDatas(int year, int month, int day, int storeid, List<NetMarketingKpiDayData> days)
        {
            var datas = GetDataQuery().Where(t => t.Date.Year == year && t.Date.Month == month && t.Date.Day == day && t.StoreId == storeid);
            foreach (var data in datas)
            {
                this.GetSet<NetMarketingKpiDayData>().Remove(data);
            }
            foreach (var config in days)
            {
                this.GetSet<NetMarketingKpiDayData>().Add(config);
            }
        }

        public void AddNetMarketingMedia(NetMarketingMedia model)
        {
            this.Valid(model, ValidMediaAdd);
            this.GetSet<NetMarketingMedia>().Add(model);
        }

        public void ModifyNetMarketingMedia(NetMarketingMedia model)
        {
            NetMarketingMedia oldmodel = GetMediaQuery().First(t => t.Id == model.Id);
            oldmodel.TypeValue = model.TypeValue;
            oldmodel.StoreId = model.StoreId;
            oldmodel.Name = model.Name;
            this.Valid(oldmodel, ValidMediaModify);
        }

        public void DeleteNetMarketingMedia(int id)
        {
            NetMarketingMedia model = GetMediaQuery().First(t => t.Id == id);
            if (model == null) return;
            this.Valid(model, ValidMediaDelete);
            this.GetSet<NetMarketingMedia>().Remove(model);
        }

        //public NetMarketingMonthData GetNetMarketingMonthData(int year, int month, int storeid, bool onlycomfirmd = false)
        //{
        //    if (onlycomfirmd)
        //    {
        //        return GetQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid && t.IsConfirmed);
        //    }
        //    else
        //    {
        //        return GetQuery().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month && t.StoreId == storeid);
        //    }
        //}

        //public IQueryable<NetMarketingMonthData> GetNetMarketingMonthDatas(int year, int storeid, bool onlycomfirmd = false)
        //{
        //    if (onlycomfirmd)
        //    {
        //        return GetQuery().Where(t => t.Month.Year == year && t.StoreId == storeid && t.IsConfirmed);
        //    }
        //    else
        //    {
        //        return GetQuery().Where(t => t.Month.Year == year && t.StoreId == storeid);
        //    }
        //}

        //public void SubmitExhibitionWeekData(NetMarketingMonthData monthdata)
        //{
        //    var datas = GetQuery().Where(t => t.Month.Year == monthdata.Month.Year && t.Month.Month == monthdata.Month.Month && t.StoreId == monthdata.StoreId);
        //    foreach (var data in datas)
        //    {
        //        this.GetSet<NetMarketingMonthData>().Remove(data);
        //    }
        //    this.GetSet<NetMarketingMonthData>().Add(monthdata);
            
        //}

        #region 标准
        public NetMarketingStandard SaveNetMarketingStandard(NetMarketingStandard model)
        {
            var standard = this.GetQuery<NetMarketingStandard>().FirstOrDefault(t => t.Id == model.Id);
            if (standard != null)
            {
                standard.FTZXLV = model.FTZXLV;
                standard.GZDBHLV = model.GZDBHLV;
                standard.GZTCLV = model.GZTCLV;
                standard.XXZHLV = model.XXZHLV;
                standard.WLZJDWL = model.WLZJDWL;
            }
            return standard;
        }

        public NetMarketingStandard GetNetMarketingStandard(int year, int month,int storeid)
        {
            var model = this.GetQuery<NetMarketingStandard>().FirstOrDefault(t => t.Month.Year == year && t.Month.Month == month&&t.StoreId==storeid);
            if (model == null)
            {
                model = new NetMarketingStandard();
                model.Month = new DateTime(year, month, 1);
                model.StoreId = storeid;
                var date = new DateTime(year, month, 1).AddMonths(-1);
                var lyear = date.Year;
                var lmonth = date.Month;
                var lastmodel = this.GetQuery<NetMarketingStandard>().FirstOrDefault(t => t.Month.Year == lyear && t.Month.Month == lmonth && t.StoreId == storeid);
                if (lastmodel != null)
                {
                    model.FTZXLV = lastmodel.FTZXLV;
                    model.GZDBHLV = lastmodel.GZDBHLV;
                    model.GZTCLV = lastmodel.GZTCLV;
                    model.XXZHLV = lastmodel.XXZHLV;
                    model.WLZJDWL = lastmodel.WLZJDWL;
                }
                this.GetSet<NetMarketingStandard>().Add(model);
            }
            return model;
        }
        #endregion


    }
}
